<?php
require('../../lib/base.php');
$base=new base(array('web','advertiser'));
$C=$base->vars('app','processor');
$log=$base->model('log');
$environment['log']=reset($log->Glog(array('where'=>array('member='=>0,'label='=>'data.processor'),'order'=>array('date'=>'DESC'),'limit'=>array(0,1))));
$data=$base->model('data');
$today=(((int)(($base->runtime+28800)/86400))*86400)-28800;
if(empty($environment['log'])){
	$start=reset($data->select('pageview',array('select'=>'date','order'=>array('date'=>'ASC'),'limit'=>array(0,1))));
	$environment['mix']=$start['date'];
	$environment['date']=(((int)(($start['date']+28800)/86400))*86400)-28800;
	if($environment['date']==$today){
		$environment['max']=$base->runtime-$C['time'];
	}else{
		$environment['max']=$environment['date']+86400;
	}
}else{
	$environment['log']['content']=preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'",$environment['log']['content']);
	$environment=unserialize($environment['log']['content']);
	if($today==$environment['date']){
		$environment['mix']=$environment['max'];
		$environment['max']=$base->runtime-$C['time'];
	}else{
		$environment['mix']=$environment['max'];
		$environment['date']=(((int)(($environment['mix']+28800)/86400))*86400)-28800;
		if(($environment['date']+86400)>$base->runtime){
			$environment['max']=$base->runtime-$C['time'];
		}else{
			$environment['max']=$environment['date']+86400;
		}
	}
}
$mysql=$base->library('mysql');
$mysql->startTrans();
$affected=$data->table('pageview',$environment['mix'],$environment['max'],$environment['date']);
if($affected===true){
	$affected=$data->table('track',$environment['mix'],$environment['max'],$environment['date']);
	if($affected===true){
		$affected=$data->table('transform',$environment['mix'],$environment['max'],$environment['date']);
		if($affected===true){
			$affected=$data->chart($environment['mix'],$environment['max'],$environment['date']);
			if($affected===true){
				$affected=$data->table('unite',$environment['mix'],$environment['max'],$environment['date']);
				if($affected===true){
					$environment['result']='SUCCESS';
				}else{
					$environment['type']='unite';
					$environment['result']='FAIL';
				}
			}else{
				$environment['type']='chart';
				$environment['result']='FAIL';
			}
		}else{
			$environment['type']='transform';
			$environment['result']='FAIL';
		}
	}else{
		$environment['type']='track';
		$environment['result']='FAIL';
	}
}else{
	$environment['type']='pageview';
	$environment['result']='FAIL';
}
if($environment['result']=='SUCCESS'){
	$affected=$log->ILog(array('member'=>0,'label'=>'data.processor','date'=>$base->runtime,'ip'=>$base->server('SERVER_ADDR'),'content'=>serialize($environment)));
	if($affected==1||true){
		$mysql->commit();
	}else{
		$environment['type']='LOG';
		$environment['result']='FAIL';
		$mysql->rollback();
	}
}else{
		$mysql->rollback();
}
print_r($environment);
?>